//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package otd;

import forge_sandbox.Sandbox;
import forge_sandbox.com.someguyssoftware.dungeons2.config.ModConfig;
import forge_sandbox.com.someguyssoftware.dungeons2.spawner.SpawnSheetLoader;
import forge_sandbox.com.someguyssoftware.dungeons2.style.StyleSheetLoader;
import forge_sandbox.greymerk.roguelike.dungeon.Dungeon;
import forge_sandbox.jaredbgreat.dldungeons.themes.ThemeReader;
import forge_sandbox.jaredbgreat.dldungeons.themes.ThemeType;
import forge_sandbox.team.cqr.cqrepoured.boss.CastleKing;
import forge_sandbox.twilightforest.structures.lichtower.boss.Lich;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldInitEvent;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.UnknownDependencyException;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import otd.MultiVersion.Version;
import otd.addon.com.ohthedungeon.storydungeon.PerPlayerDungeonInstance;
import otd.commands.Otd;
import otd.commands.Otd_Cp;
import otd.commands.Otd_Place;
import otd.commands.Otd_Reload;
import otd.commands.Otd_Tp;
import otd.config.WorldConfig;
import otd.config.YamlPluginConfig;
import otd.config.WorldConfig.AsyncSaver;
import otd.dungeon.draylar.BattleTowerSchematics;
import otd.gui.AetherDungeonConfig;
import otd.gui.AntManDungeonConfig;
import otd.gui.BackupGUI;
import otd.gui.BattleTowerConfig;
import otd.gui.BiomeSetting;
import otd.gui.BossConfig;
import otd.gui.CastleDungeonConfig;
import otd.gui.DoomlikeConfig;
import otd.gui.DraylarBattleTowerConfig;
import otd.gui.DungeonSpawnSetting;
import otd.gui.LichTowerConfig;
import otd.gui.LootItem;
import otd.gui.LootManager;
import otd.gui.MainMenu;
import otd.gui.RogueLikeDungeonTower;
import otd.gui.RoguelikeConfig;
import otd.gui.RoguelikeLootItem;
import otd.gui.RoguelikeLootManager;
import otd.gui.SmoofyConfig;
import otd.gui.WorldEditor;
import otd.gui.WorldManager;
import otd.gui.WorldSpawnerManager;
import otd.gui.customstruct.CustomDungeonEditor;
import otd.gui.customstruct.CustomDungeonList;
import otd.gui.customstruct.CustomDungeonPlaceSelect;
import otd.gui.customstruct.MobSelect;
import otd.gui.customstruct.SchematicSelect;
import otd.gui.customstruct.WorldCustomDungeon;
import otd.gui.dungeon_plot.CreateDungeonWorld;
import otd.gui.dungeon_plot.RemoveDungeonWorld;
import otd.gui.dungeon_plot.UserTeleport;
import otd.gui.storydungeon.PPDI_Config;
import otd.integration.BossImpl;
import otd.integration.MythicMobsImpl;
import otd.integration.PlaceholderAPI;
import otd.integration.WorldEdit;
import otd.lib.async.AsyncRoguelikeDungeon;
import otd.lib.async.io.papermc.lib.PaperLib;
import otd.lib.bstats.Metrics;
import otd.listener.MobListener;
import otd.listener.SpawnerListener;
import otd.populator.DungeonPopulator;
import otd.struct.SchematicLoader;
import otd.update.UpdateChecker;
import otd.util.Diagnostic;
import otd.util.ExceptionReporter;
import otd.util.I18n;
import otd.util.LanguageUtil;
import otd.util.Logging;
import otd.world.ChunkList;
import otd.world.DungeonWorld;
import otd.world.WorldGenOptimization;
import top.yunmouren.Oh_the_dungeons_youll_go;

public class Main extends JavaPlugin {
    public static JavaPlugin instance;
    public static Main mainInstance;
    public static boolean disabled = false;
    private static Integer api_version = 6;
    public static Version version;
    private Metrics metrics;
    private static final int metric_pluginId = 9213;
    private static PerPlayerDungeonInstance ppdi;
    public static final boolean DEBUG = true;
    private BukkitRunnable update_check_task_id;
    private final int RESOURCE_ID = 76437;

    static {
        version = Version.UNKNOWN;
    }

    public Main() {
        instance = this;
        mainInstance = this;
        version = Version.V1_18_R1;
        Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You'll Go] MC Version: 1.18.[0-1]", ChatColor.GREEN);
        MultiVersion.has3DBiome();
        if (version == Version.UNKNOWN) {
            throw new UnsupportedOperationException("Unknown server version...");
        } else {
            MultiVersion.init();
            Sandbox.mkdir();
            BackupGUI.initBackupFolder();
        }
    }

    public void onDisable() {
        AsyncSaver.cancelAsyncSaver();
        WorldConfig.close();
        Bukkit.getLogger().log(Level.WARNING, "[Oh The Dungeons You'll Go] Plugin is disabled");
        disabled = true;
    }

    public void onEnable() {
        Oh_the_dungeons_youll_go.main();
        try {
            Class.forName("org.spigotmc.SpigotConfig");
        } catch (ClassNotFoundException var3) {
            this.getLogger().severe("[Oh The Dungeons You'll Go] requires Spigot (or a fork such as Paper) in order to run.");
            throw new UnsupportedOperationException("Unsupported Server Type");
        }

        try {
            YamlPluginConfig.init();
        } catch (IOException var2) {
            Bukkit.getLogger().log(Level.SEVERE, ExceptionReporter.exceptionToString(var2));
        }

        disabled = false;
        I18n.init();
        (new File(this.getDataFolder(), "schematics")).mkdirs();
        if (WorldEdit.isReady()) {
            SchematicLoader.initDir(this);
        }

        WorldConfig.loadWorldConfig();
        AsyncSaver.setupAsyncSaver();
        ThemeReader.setConfigDir();
        ThemeReader.setThemesDir();
        ThemeReader.readSpecialChest();
        ThemeReader.readThemes();
        ThemeType.SyncMobLists();
        StyleSheetLoader.exposeStyleSheet(ModConfig.styleSheetFile);
        SpawnSheetLoader.exposeSpawnSheet(ModConfig.spawnSheetFile);
        Dungeon.init = true;
        this.getServer().getPluginManager().registerEvents(new Main.DLDWorldListener(), this);
        this.getServer().getPluginManager().registerEvents(WorldEditor.instance, this);
        this.getServer().getPluginManager().registerEvents(WorldManager.instance, this);
        this.getServer().getPluginManager().registerEvents(RoguelikeConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(LootManager.instance, this);
        this.getServer().getPluginManager().registerEvents(LootItem.instance, this);
        this.getServer().getPluginManager().registerEvents(RoguelikeLootManager.instance, this);
        this.getServer().getPluginManager().registerEvents(RoguelikeLootItem.instance, this);
        this.getServer().getPluginManager().registerEvents(BiomeSetting.instance, this);
        this.getServer().getPluginManager().registerEvents(DoomlikeConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(BattleTowerConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(DungeonSpawnSetting.instance, this);
        this.getServer().getPluginManager().registerEvents(SmoofyConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(DraylarBattleTowerConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(WorldSpawnerManager.instance, this);
        this.getServer().getPluginManager().registerEvents(AntManDungeonConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(AetherDungeonConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(LichTowerConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(MainMenu.instance, this);
        this.getServer().getPluginManager().registerEvents(RemoveDungeonWorld.instance, this);
        this.getServer().getPluginManager().registerEvents(CreateDungeonWorld.instance, this);
        this.getServer().getPluginManager().registerEvents(UserTeleport.instance, this);
        this.getServer().getPluginManager().registerEvents(BackupGUI.instance, this);
        this.getServer().getPluginManager().registerEvents(RogueLikeDungeonTower.instance, this);
        this.getServer().getPluginManager().registerEvents(CustomDungeonEditor.instance, this);
        this.getServer().getPluginManager().registerEvents(CustomDungeonList.instance, this);
        this.getServer().getPluginManager().registerEvents(MobSelect.instance, this);
        this.getServer().getPluginManager().registerEvents(SchematicSelect.instance, this);
        this.getServer().getPluginManager().registerEvents(WorldCustomDungeon.instance, this);
        this.getServer().getPluginManager().registerEvents(CustomDungeonPlaceSelect.instance, this);
        this.getServer().getPluginManager().registerEvents(PPDI_Config.instance, this);
        this.getServer().getPluginManager().registerEvents(CastleDungeonConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(BossConfig.instance, this);
        this.getServer().getPluginManager().registerEvents(new MobListener(), this);
        this.getServer().getPluginManager().registerEvents(new SpawnerListener(), this);
        this.getServer().getPluginManager().registerEvents(new Lich(), this);
        this.getServer().getPluginManager().registerEvents(new CastleKing(), this);
        this.getServer().getPluginManager().registerEvents(new WorldGenOptimization(), this);
        ChunkList.rebuildChunkMap();
        if (YamlPluginConfig.update) {
            Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You''ll Go] Update checking...", ChatColor.GREEN);
            this.asyncUpdateChecker();
        }

        this.metrics = new Metrics(this, 9213);
        this.registerCommand();
        BattleTowerSchematics.init(this);
        LanguageUtil.init();
        Lich.init();
        CastleKing.init();
        PlaceholderAPI.enable();
        MythicMobsImpl.enable();
        BossImpl.enable();
        AsyncRoguelikeDungeon.init();
        Bukkit.getScheduler().runTaskLater(instance, () -> {
            Diagnostic.diagnostic();

            try {
                InputStream stream = this.getResource("logo.txt");
                InputStreamReader isr = new InputStreamReader(stream);
                BufferedReader reader = new BufferedReader(isr);

                String line;
                while((line = reader.readLine()) != null) {
                    Bukkit.getLogger().log(Level.INFO, "{0}{1}", new Object[]{ChatColor.BLUE, line});
                }
            } catch (IOException var5) {
            }

        }, 2L);
        Bukkit.getScheduler().runTaskLater(this, () -> {
            if (!PaperLib.isPaper()) {
                Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You'll Go] You are not using Paper, async chunk generator is disabled. Dungeon generation may cause tps loss", ChatColor.RED);
            }

            if (!WorldEdit.isReady()) {
                Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You'll Go] WorldEdit not installed, custom dungeon function is disabled. Don't worry, you could still use the built-in dungeons", ChatColor.RED);
            }

            if (!PlaceholderAPI.isReady()) {
                Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You'll Go] PlaceHolderAPI not installed, all placeholders are disabled", ChatColor.RED);
            }

            if (!MythicMobsImpl.isMythicMobsReady()) {
                Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You'll Go] MythicMobs not installed, will disable MythicMobs related features", ChatColor.RED);
            }

            if (!BossImpl.isBossReady()) {
                Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You'll Go] Boss not installed, will disable BossPlugin related features", ChatColor.RED);
            }

        }, 3L);
        Bukkit.getScheduler().runTaskLater(this, () -> {
            if (WorldConfig.wc.dungeon_world.finished) {
                Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You'll Go] Loading dungeon plot world...", ChatColor.GREEN);
                DungeonWorld.loadDungeonWorld();
            }

        }, 1L);
        Bukkit.getScheduler().runTaskLater(this, () -> {
            Bukkit.getLogger().log(Level.INFO, "{0}[Oh The Dungeons You'll Go] Loading PerPlayerDungeonInstance...", ChatColor.GREEN);
            ppdi = new PerPlayerDungeonInstance();
        }, 1L);
    }

    private void loadPDF() {
        File out = new File(instance.getDataFolder(), "OTD.pdf");

        try {
            Throwable var2 = null;
            Object var3 = null;

            try {
                InputStream in = instance.getResource("OTD.pdf");

                try {
                    BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(out, false));

                    try {
                        byte[] buffer = new byte[4096];

                        int length;
                        while((length = in.read(buffer)) >= 0) {
                            writer.write(buffer, 0, length);
                        }
                    } finally {
                        if (writer != null) {
                            writer.close();
                        }

                    }
                } catch (Throwable var25) {
                    if (var2 == null) {
                        var2 = var25;
                    } else if (var2 != var25) {
                        var2.addSuppressed(var25);
                    }

                    if (in != null) {
                        in.close();
                    }

                    throw var2;
                }

                if (in != null) {
                    in.close();
                }
            } catch (Throwable var26) {
                if (var2 == null) {
                    var2 = var26;
                } else if (var2 != var26) {
                    var2.addSuppressed(var26);
                }

                throw var2;
            }
        } catch (Throwable var27) {
            Bukkit.getLogger().log(Level.SEVERE, "Load OTD.pdf error...");
            return;
        }

        try {
            this.getServer().getPluginManager().loadPlugin(out);
        } catch (InvalidPluginException | UnknownDependencyException | InvalidDescriptionException var23) {
            Bukkit.getLogger().log(Level.SEVERE, "Load OTD.pdf error...");
        }

    }

    private void loadAdvancement() {
        File out = new File(instance.getDataFolder(), "OhTheDungeonAdvancement.jar");

        try {
            Throwable var2 = null;
            Object var3 = null;

            try {
                InputStream in = instance.getResource("OhTheDungeonAdvancement.jar");

                try {
                    BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(out, false));

                    try {
                        byte[] buffer = new byte[4096];

                        int length;
                        while((length = in.read(buffer)) >= 0) {
                            writer.write(buffer, 0, length);
                        }
                    } finally {
                        if (writer != null) {
                            writer.close();
                        }

                    }
                } catch (Throwable var25) {
                    if (var2 == null) {
                        var2 = var25;
                    } else if (var2 != var25) {
                        var2.addSuppressed(var25);
                    }

                    if (in != null) {
                        in.close();
                    }

                    throw var2;
                }

                if (in != null) {
                    in.close();
                }
            } catch (Throwable var26) {
                if (var2 == null) {
                    var2 = var26;
                } else if (var2 != var26) {
                    var2.addSuppressed(var26);
                }

                throw var2;
            }
        } catch (Throwable var27) {
            Bukkit.getLogger().log(Level.SEVERE, "Load Advancements error...");
            return;
        }

        try {
            this.getServer().getPluginManager().loadPlugin(out);
        } catch (InvalidPluginException | UnknownDependencyException | InvalidDescriptionException var23) {
            Bukkit.getLogger().log(Level.SEVERE, "Load Advancements error...");
        }

    }

    private void asyncUpdateChecker() {
        this.update_check_task_id = new BukkitRunnable() {
            public void run() {
                UpdateChecker.CheckUpdate(Main.instance, 76437);
            }
        };
        this.update_check_task_id.runTaskTimerAsynchronously(this, 200L, 72000L);
    }

    private void registerCommand() {
        Otd otd = new Otd();
        Otd_Place otd_place = new Otd_Place();
        Otd_Cp otd_cp = new Otd_Cp();
        Otd_Tp otd_tp = new Otd_Tp();
        Otd_Reload otr = new Otd_Reload();
        PluginCommand command = this.getCommand("oh_the_dungeons");
        if (command != null) {
            command.setExecutor(otd);
            command.setTabCompleter(otd);
        }

        command = this.getCommand("oh_the_dungeons_place");
        if (command != null) {
            command.setExecutor(otd_place);
            command.setTabCompleter(otd_place);
        }

        command = this.getCommand("oh_the_dungeons_cp");
        if (command != null) {
            command.setExecutor(otd_cp);
            command.setTabCompleter(otd_cp);
        }

        command = this.getCommand("oh_the_dungeons_tp");
        if (command != null) {
            command.setExecutor(otd_tp);
            command.setTabCompleter(otd_tp);
        }

        command = this.getCommand("otd_reload");
        if (command != null) {
            command.setExecutor(otr);
            command.setTabCompleter(otr);
        }

    }

    private class DLDWorldListener implements Listener {
        private DLDWorldListener() {
        }

        @EventHandler(
                priority = EventPriority.LOW
        )
        public void onWorldInit(WorldInitEvent event) {
            String world_name = event.getWorld().getName();
            if (!world_name.equals("otd_dungeon")) {
                if (!world_name.equalsIgnoreCase("otd_dungeon_shadow_world")) {
                    Logging.logInfo(ChatColor.BLUE + "[Oh The Dungeons You'll Go] Found world: " + world_name);
                    event.getWorld().getPopulators().add(new DungeonPopulator());
                }
            }
        }
    }
}
